#include <iostream>
#include "funlib.h"
#include "spline.cpp"
#include "jsonprocess.cpp"
#include "tool.hpp"

using namespace std;
namespace fs = std::filesystem;

void problemA()
{
    json jsonArray;
    vector<int> num{6, 11, 21, 41, 81};
    constexpr int dim = 1;
    for (int i = 0; i < num.size(); ++i)
    {
        json jsonData;
        int N = num[i];
        double h = 2.0 / (N - 1);
        vector<vector<double>> knots_curve(dim + 1, vector<double>(N, 0));
        for (int j = 0; j < N; ++j)
        {
            knots_curve[0][j] = -1 + j * h;
            knots_curve[1][j] = f1(knots_curve[0][j]);
        }
        vector<vector<double>> bc{vector<double>{df1(-1), df1(1)}};

        jsonData["curve"] = knots_curve;
        jsonData["bctp"] = static_cast<int>(Complete);
        jsonData["bc"] = bc;
        jsonArray.push_back(jsonData);
    }
    const char *filename = "jsonfile/problemA.json";
    std::ofstream file(filename);
    if (!file.is_open())
    {
        throw std::runtime_error("文件夹打开失败");
    }
    file << std::setw(4) << jsonArray << std::endl;
    file.close();
    std::cout << "JSON 数据已写入文件" << std::endl;
}

void problemBCD()
{
    json jsonArray;
    int N = 11;
    constexpr int dim = 1;

    double h = 1;
    json jsonData;
    vector<vector<double>> knots_curve(dim + 1, vector<double>(N, 0));
    for (int j = 0; j < N; ++j)
    {
        knots_curve[0][j] = -5 + j * h;
        knots_curve[1][j] = f2(knots_curve[0][j]);
    }
    vector<vector<double>> bc{vector<double>{df2(-5), df2(5)}};

    jsonData["curve"] = knots_curve;
    jsonData["bctp"] = static_cast<int>(Complete);
    jsonData["bc"] = bc;
    jsonArray.push_back(jsonData);

    knots_curve = vector<vector<double>>(dim + 1, vector<double>(N - 1, 0));
    for (int j = 0; j < N - 1; ++j)
    {
        knots_curve[0][j] = -4.5 + j * h;
        knots_curve[1][j] = f2(knots_curve[0][j]);
    }
    bc = vector<vector<double>>{vector<double>{f2(-5), f2(5)}};

    jsonData["curve"] = knots_curve;
    jsonData["bctp"] = static_cast<int>(End);
    jsonData["bc"] = bc;
    jsonArray.push_back(jsonData);

    const char *filename = "jsonfile/problemBCD.json";
    std::ofstream file(filename);
    if (!file.is_open())
    {
        throw std::runtime_error("文件夹打开失败");
    }
    file << std::setw(4) << jsonArray << std::endl;
    file.close();
    std::cout << "JSON 数据已写入文件" << std::endl;
}

void problemE()
{
    json jsonArray;
    vector<int> num{11, 41, 161};
    constexpr int dim = 2;
    for (int i = 0; i < num.size(); ++i)
    {
        json jsonData;
        int N = num[i];
        double h = 2.0 * M_PI / (N - 1);
        vector<vector<double>> knots_curve(dim, vector<double>(N, 0));
        for (int j = 0; j < N; ++j)
        {
            knots_curve[0][j] = f_xt(j * h);
            knots_curve[1][j] = f_yt(j * h);
        }
        jsonData["curve"] = knots_curve;
        jsonData["bctp"] = static_cast<int>(Periodic);
        jsonArray.push_back(jsonData);
    }
    const char *filename = "jsonfile/problemE.json";
    std::ofstream file(filename);
    if (!file.is_open())
    {
        throw std::runtime_error("文件夹打开失败");
    }
    file << std::setw(4) << jsonArray << std::endl;
    file.close();
    std::cout << "JSON 数据已写入文件" << std::endl;
}

int main()
{
    createFilepathIfNotExist("jsonfile");
    problemA();
    problemBCD();
    problemE();
    return 0;
}